<template>
  <div id="app">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader @appAddTask="appAddTask"></MyHeader>
        <MyList :tasks="tasks"></MyList>
        <MyFooter :tasks="tasks"
                  @appModifyAll="appModifyAll"
                  @appClearAllDoneTask="appClearAllDoneTask"></MyFooter>
      </div>
    </div>
  </div>
</template>

<script>
import pubsub from "pubsub-js";
import MyHeader from './components/MyHeader'
import MyFooter from './components/MyFooter'
import MyList from "./components/MyList/MyList";

export default {
  name: 'App',
  components: {
    MyHeader,
    MyFooter,
    MyList
  },
  data() {
    return {
      tasks: JSON.parse(localStorage.getItem('tasks')) || [],
    }
  },
  watch: {
    'tasks': {
      immediate: false,
      deep: true,
      handler(valNew) {
        localStorage.setItem('tasks', JSON.stringify(valNew));
      }
    }
  },
  methods: {
    appAddTask(task) {
      this.tasks.unshift(task);
    },
    /**
     * 改变task的完成状态
     * @param id
     */
    appDoneCheck(id) {
      this.tasks.forEach((t) => {
        if (t.id === id) {
          t.done = !t.done;
        }
      });
    },
    /**
     *
     * @param id {String}
     */
    appDeleteTask(id) {
      this.tasks = this.tasks.filter((t) => {
        return t.id !== id;
      });
    },

    /**
     *
     * @param flag {Boolean}
     */
    appModifyAll(flag) {
      this.tasks.forEach((t) => {
        t.done = flag;
      });
    },
    appClearAllDoneTask() {
      this.tasks = this.tasks.filter((t) => {
        return !t.done;
      });
    },
    appEditTitle(id, title){
      this.tasks.forEach((t) => {
        if (t.id === id) {
          t.title = title;
        }
      });
    }
  },
  mounted() {
    // this.$bus.$on('appDoneCheck', this.appDoneCheck);
    // this.$bus.$on('appDeleteTask', this.appDeleteTask);
    this.$bus.$on('editTitle', this.appEditTitle);
    this.pubid_appDoneCheck = pubsub.subscribe('appDoneCheck', (_, id) => {
      this.appDoneCheck(id);
    });
    this.pubid_appDeleteTask = pubsub.subscribe('appDeleteTask', (_, id) => {
      this.appDeleteTask(id);
    });
  },
  beforeDestroy() {
    // this.$bus.$off('appDoneCheck');
    // this.$bus.$off('appDeleteTask');
    pubsub.unsubscribe(this.pubid_appDoneCheck);
    pubsub.unsubscribe(this.pubid_appDeleteTask);
  }
}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
